<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
      <input name="id" hidden/>
        <div class="layui-form-item">
      <label class="layui-form-label">油站名称</label>
      <div class="layui-input-block">
        <input type="regionName" name="regionName" placeholder="请输入区域名称" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">销售额</label>
      <div class="layui-input-block">
        <input type="sale" name="sale" placeholder="请输入销售额" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">排名</label>
      <div class="layui-input-block">
        <input type="rank" name="rank" placeholder="请输入排名" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">年月</label>
      <div class="layui-input-block">
        <input type="yearMonth" name="yearMonth" placeholder="请输入年月" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">field1</label>
      <div class="layui-input-block">
        <input type="field1" name="field1" placeholder="请输入field1" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">field2</label>
      <div class="layui-input-block">
        <input type="field2" name="field2" placeholder="请输入field2" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">field3</label>
      <div class="layui-input-block">
        <input type="field3" name="field3" placeholder="请输入field3" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">field4</label>
      <div class="layui-input-block">
        <input type="field4" name="field4" placeholder="请输入field4" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">field5</label>
      <div class="layui-input-block">
        <input type="field5" name="field5" placeholder="请输入field5" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">是否删除(1未删除；0已删除)</label>
      <div class="layui-input-block">
        <input type="deleted" name="deleted" placeholder="请输入是否删除(1未删除；0已删除)" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">创建人</label>
      <div class="layui-input-block">
        <input type="createId" name="createId" placeholder="请输入创建人" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">更新人</label>
      <div class="layui-input-block">
        <input type="updateId" name="updateId" placeholder="请输入更新人" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">创建来源(1.web 2.android 3.ios )</label>
      <div class="layui-input-block">
        <input type="createWhere" name="createWhere" placeholder="请输入创建来源(1.web 2.android 3.ios )" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">创建时间</label>
      <div class="layui-input-block">
        <input type="createTime" name="createTime" placeholder="请输入创建时间" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label"></label>
      <div class="layui-input-block">
        <input type="updateTime" name="updateTime" placeholder="请输入" autocomplete="off" class="layui-input">
      </div>
    </div>
  <div class="layui-form-item">
  <div class="layui-input-block">
    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
  </div>
</div>
</form>
</div>
</div>

<div class="table_div">
  <div id="searchParam" >
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input type="text" id="regionNameInput" class="layui-input"  autocomplete="off" placeholder="请输入区域名称">
      </div>
	  
	  <div class="layui-input-inline">
        <input type="text" id="yearMonthInput" class="layui-input"  autocomplete="off" placeholder="请输入年月">
      </div>
	  
      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search()"  id="search">查询</button>
        <button class="layui-btn" id="export">导出</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<script type="text/html" id="toolbar">
  <!-- 
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add"  >添加</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" >删除</button>
  </div>
  -->
</script>
<script type="text/html" id="tool">
  <!--
  <a class="layui-btn layui-btn-xs" lay-event="edit" >编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除</a>
  -->
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var $ = jQuery = layui.jquery;
  //初始当前 年月
  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1; // 月份从0开始，所以要加1
  var yearMonthInput;
  if(10 > currentMonth > 0){
	  yearMonthInput = currentYear + "0" + currentMonth;
	  $("#yearMonthInput").val(currentYear + "0" + currentMonth);
  }else{
	  yearMonthInput = currentYear + "" + currentMonth;
	  $("#yearMonthInput").val(currentYear + "" + currentMonth);
  }
  
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var laydate = layui.laydate;

  layui.use(['table', 'layer', 'laydate'], function () {
	  laydate.render({
		    elem: '#yearMonthInput',
		    type: 'month', // 设置类型为年选择
		    format: 'yyyyMM', // 设置年月格式
		    trigger: 'click', // 设置触发方式
		    /* ready: function(date){
		      // 控制器初始化时触发
		    },
		    change: function(value, date, endDate){
		      // 日期时间被切换时触发
		    } */
		  });
    //加载table
    tableIns1 = table.render({
      elem: '#showTable'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: true //开启分页
      , url: '/gas/analyse/regionranking/list' //数据接口
      , where: {yearMonths: yearMonthInput} // 初始化时的过滤条件

      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: '主键', sort: true, hide: true},
          {field: 'regionName', title: '区域名称', sort: true},
          {field: 'sale', title: '累计销售额', sort: true},
          {field: 'rank', title: '排名', sort: true},
          {field: 'yearMonths', title: '年月', sort: true},
          /* {field: 'field1', title: 'field1', sort: true},
          {field: 'field2', title: 'field2', sort: true},
          {field: 'field3', title: 'field3', sort: true},
          {field: 'field4', title: 'field4', sort: true},
          {field: 'field5', title: 'field5', sort: true},
          {field: 'deleted', title: '是否删除(1未删除；0已删除)', sort: true},
          {field: 'createId', title: '创建人', sort: true},
          {field: 'updateId', title: '更新人', sort: true},
          {field: 'createWhere', title: '创建来源(1.web 2.android 3.ios )', sort: true},
          {field: 'createTime', title: '创建时间', sort: true},
          {field: 'updateTime', title: '', sort: true},
          {width: 120, toolbar: "#tool", title: '操作'} */
        ]
      ]
      , toolbar: '#toolbar'
    });


    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
            layer.msg("请选择要批量删除的列");
          }else {
            var ids = [];
            $(data).each(function (index,item) {
              ids.push(item.id);
            });
            tipDialog(ids);
          }
          break;
        case 'add':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("新增");
          $(".operation input[name=id]").val("");
          $(".operation input[name=regionName]").val("");
          $(".operation input[name=sale]").val("");
          $(".operation input[name=rank]").val("");
          $(".operation input[name=yearMonth]").val("");
          $(".operation input[name=field1]").val("");
          $(".operation input[name=field2]").val("");
          $(".operation input[name=field3]").val("");
          $(".operation input[name=field4]").val("");
          $(".operation input[name=field5]").val("");
          $(".operation input[name=deleted]").val("");
          $(".operation input[name=createId]").val("");
          $(".operation input[name=updateId]").val("");
          $(".operation input[name=createWhere]").val("");
          $(".operation input[name=createTime]").val("");
          $(".operation input[name=updateTime]").val("");
          break;
      };
    });
    //列操作
    table.on('tool(showTable)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'del':
          var ids=[];
          ids.push(data.id);
          tipDialog(ids);
          break;
        case 'edit':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("编辑");
          $(".operation input[name=id]").val(data.id);
          $(".operation input[name=regionName]").val(data.regionName);
          $(".operation input[name=sale]").val(data.sale);
          $(".operation input[name=rank]").val(data.rank);
          $(".operation input[name=yearMonth]").val(data.yearMonth);
          $(".operation input[name=field1]").val(data.field1);
          $(".operation input[name=field2]").val(data.field2);
          $(".operation input[name=field3]").val(data.field3);
          $(".operation input[name=field4]").val(data.field4);
          $(".operation input[name=field5]").val(data.field5);
          $(".operation input[name=deleted]").val(data.deleted);
          $(".operation input[name=createId]").val(data.createId);
          $(".operation input[name=updateId]").val(data.updateId);
          $(".operation input[name=createWhere]").val(data.createWhere);
          $(".operation input[name=createTime]").val(data.createTime);
          $(".operation input[name=updateTime]").val(data.updateTime);
          break;
      }
    });

    //导出
    $('#export').on('click', function () {
      //原先分页limit
      var exportParams = {
        limit: -1,
        key: $("#key").val(),
        regionName: $("#regionNameInput").val(),
  	  	yearMonths: $("#yearMonthInput").val()
      };
      CoreUtil.sendPost("/gas/analyse/regionranking/list", exportParams, function (res) {
        //初始化渲染数据
        if (res.data != null && res.data.records != null) {
          table.exportFile(tableIns1.config.id, res.data.records, 'xls');
        }
      });
    });

    //删除
    var tipDialog=function (ids) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete("/gasAnalyseRegionRanking/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              search();
            });
          });
        }
      });
    };

    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost("/gasAnalyseRegionRanking/add",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }else {
        CoreUtil.sendPut("/gasAnalyseRegionRanking/update",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }

      return false;
    });
  });

  //执行查询
  function search() {
    //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
    	  regionName: $("#regionNameInput").val(),
    	  yearMonths: $("#yearMonthInput").val()

      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
</script>